<?php

class Port extends Wp_port_model {

    function __construct() {
        parent::__construct();
    }
    
    function getList($filter = array()) {
        $port = new Port();
        if(isset($filter['name']) && $filter['name']) {
            $port->addWhere("wp_port.name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['country']) && $filter['country']) {
            $port->addWhere("wp_port.id_wp_country = ".intval($filter['country']));
        }
        
        $port->addJoin(new Country());
        $port->addWhere('wp_port.is_deleted = 0');
        $port->addSelect();
        $port->addSelect("wp_port.id, wp_port.name, wp_country.name as country_name");
        $port->orderBy("wp_port.name");
        $port->find();
        
        return $port;            
    }
    
    function isDeleteAble() {
        $port = new Port();
        return true;
    }

    function validateInput() {

        if (!$this->id_wp_country) {
            MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $country = new Country();
            if (!$country->get($this->id_wp_country)) {
                 MessageHandler::add (_MSG_COUNTRY_INVALID, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty ($this->name)) {
            MessageHandler::add (_MSG_NAME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
        
        if (strlen($this->name) > MAX_LENGTH_NAME) {
            MessageHandler::add (_MSG_PORT_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
        }
        
        if (self::isExistsByName($this->name,$this->id, $this->id_wp_country)) {
            MessageHandler::add (_MSG_PORT_NAME_EXISTS, MSG_ERROR, LOG_ONLY);
        }
     
        return MessageHandler::countError() > 0 ? false : true;

    }
    
    public function insert() {
        $this->created_date = date('y-m-d H:i:s');
        parent::insert();
    }
    
    public function isExistsByName($name = '', $id = 0, $id_wp_country = 0)
	{
		if ($name == '')
		{
			return false;
		}	
		$port = new Port();
		
        if ($id) {
			$port->addWhere("id <> {$id}");
		}
                
        $port->addWhere("name = '$name'");
        $port->addWhere("id_wp_country = '$id_wp_country'");
		$port->find();
		return ($port->countRows() > 0)?'1':'0';
	}
    
    public function isExistsByIds($ids = array())
	{
		if (!is_array($ids)) {
            return true;
        }
        
		$port = new Port();
		$port->addWhere("id IN (".  implode(',', $ids).")");
		$port->addWhere("is_deleted = ".IS_NOT_DELETED);
        $port->find();
		return ($port->countRows() == count($ids))?true:false;
	}
    
    public function getPorts($ids)
	{
		$port = new Port();
		$port->addWhere("id IN (".$ids.")");
		$port->addWhere("is_deleted = ".IS_NOT_DELETED);
        $port->find();
        $port_name = array();
        while($port->fetchNext()) {
            $port_name[] = $port->name;
        }
		return implode(', ', $port_name);
	}
}
